package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.R;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("listInventory")
    public Map<String,Object> getGoodsList(@RequestParam(value = "codeOrName",required = false) String codeOrName,
                                           @RequestParam(value = "goodsTypeId",required = false) String goodsTypeId,
                                           @RequestParam("page") Integer page,
                                           @RequestParam("rows") Integer rows){
        Map<String, Object> map = goodsService.getGoodsList(page, rows, codeOrName, goodsTypeId);
        return map;

    }

    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("list")
    public Map<String,Object> list(@RequestParam("page") Integer page,
                                   @RequestParam("rows") Integer rows,
                                   @RequestParam(value = "goodsName",required = false) String goodsName,
                                   @RequestParam(value = "goodsTypeId",required = false) String goodsTypeId){

        return goodsService.getGoodsPge(page,rows,goodsName,goodsTypeId);
    }


    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */
    @PostMapping("save")
    public R save(Goods goods){
        if(StringUtils.isEmpty(goods.getGoodsId()))
            goodsService.save(goods);
        else
            goodsService.updateById(goods);
        return R.ok(100,"请求成功",null);
    }

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestParam("goodsId") String goodsId){
        Goods info = goodsService.getInfoById(goodsId);
        if(info.getState() != 0){
            return R.ok(5,"当前商品已上架或有单据",null);
        }
        goodsService.deleteById(goodsId);
        return R.ok(100,"删除成功",null);
    }

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(@RequestParam("page") Integer page,
                                                     @RequestParam("rows") Integer rows,
                                                     @RequestParam(value = "nameOrCode",required = false) String nameOrCode){

        Map<String,Object> map = new HashMap<>();
        map.put("rows",goodsService.getInvenPage(page,rows,nameOrCode));
        map.put("total",goodsService.getInvenTotal());
        return  map;
    }


    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @PostMapping("getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(@RequestParam("page") Integer page,
                                                      @RequestParam("rows") Integer rows,
                                                      @RequestParam(value = "nameOrCode",required = false) String nameOrCode){
        Map<String,Object> map = new HashMap<>();
        map.put("rows",goodsService.getHashPage(page,rows,nameOrCode));
        map.put("total",goodsService.getHashTotal());
        return  map;

    }


    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */
    @PostMapping("saveStock")
    public R saveStock(@RequestParam("goodsId") String goodsId,
                       @RequestParam("inventoryQuantity") Integer inventoryQuantity,
                       @RequestParam("purchasingPrice") Double purchasingPrice){
        goodsService.updateInfo(goodsId,inventoryQuantity,purchasingPrice);
        return R.ok(100,"请求成功",null);
    }

    /**
     * 删除商品库存  将库存改成0
     * @param goodsId 商品ID
     * @return
     */
    @PostMapping("deleteStock")
    public R deleteStock(@RequestParam("goodsId") String goodsId){
        //获取详情 查看当前商品状态
        Goods infoById = goodsService.getInfoById(goodsId);
        if(0 != infoById.getState())
            return R.error(5,"商品已出库或有单据",null);

        //执行删除  将商品数量改为0
        goodsService.updateInven(goodsId);
        return R.ok(100,"请求成功",null);

    }

    /**
     * 查询库存报警商品信息
     * @return
     */
    @PostMapping("listAlarm")
    public Map<String,Object> listAlarm(){
        Map<String,Object> map = new HashMap<>();
        map.put("rows",goodsService.getListAlarm());
        return map;
    }

}
